package com.ruoyi.quartz.race.stock;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ruoyi.quartz.util.JedisUtil;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;

/**
 * 分析类
 */
public class StockUtil {
	public static void main(String[] args) {
		List<Map<String,Object>> retList=new ArrayList();
		loadPage(1,retList);
		String key = "stock" ;
		JedisUtil.set(key, JSON.toJSONString(retList));
	}

	public static String getDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmm");
        Date date = new Date();
        String formattedDate = formatter.format(date);
        return formattedDate;
    }
	
	private static void loadPage(int pn, List<Map<String, Object>> retList) {
//		System.out.println(pn);
		String url = "http://1.push2.eastmoney.com/api/qt/clist/get?cb=&pn=" + pn
				+ "&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&wbp2u=|0|0|0|web&fid=f3&fs=m:1+t:2,m:1+t:23&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_=1710253677754";
		String json = HttpUtil.get(url);
		Map<String, Object> jsonMap = JSON.parseObject(json);
		Map<String, Object> dataMap = (Map<String, Object>) jsonMap.get("data");
		List<Map<String, Object>> diffList = (List<Map<String, Object>>) dataMap.get("diff");
		boolean isnextpage = true;
		String ndate=getDate();
		for (int i = 0; i < diffList.size(); i++) {
			Map<String, Object> diffMap = diffList.get(i);
			diffMap.put("cdate", ndate);
			Float uppercent = Float.parseFloat(diffMap.get("f3").toString());
			if (uppercent == null) {
				// 跳出循环不再继续执行
				isnextpage = false;
			} else {
				if (uppercent >= 3.0) {
					if (uppercent <= 5.0) {
						// 进入下一步判断
						// 市值f20 50-200亿
						// 5000000000-20000000000
						// 换手率f8 5-10
						// 量比f10 >1
						try {
							Float sz = Float.parseFloat(tostr(diffMap.get("f20")));
							Float hsl = Float.parseFloat(tostr(diffMap.get("f8")));
							Float lb = Float.parseFloat(tostr(diffMap.get("f10")));
							if (sz >= 5000000000f && sz <= 20000000000f && hsl >= 5 && hsl <= 10 && lb >= 1) {
//								System.out.println(diffMap);
								retList.add(diffMap);
							} else {
								// 排除
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					} else {
						// 判断下一条
					}
				} else {
					// 不满足条件,继续下一个
					isnextpage = false;
				}
			}
		}
		if (isnextpage) {
			loadPage(pn + 1,retList);
		}
	}

	private static String tostr(Object obj) {
		String objStr = StrUtil.toString(obj);
		if (objStr.equals("null") || objStr.equals("-") || objStr.equals("")) {
			return "0";
		} else {
			return objStr;
		}
	}

	public static void process() {
		List<Map<String,Object>> retList=new ArrayList();
		loadPage(1,retList);
		String key = "stock" ;
		JedisUtil.set(key, JSON.toJSONString(retList));
	}
}
